# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#
validate_acct_info = (form) ->
    # 如果开户人证件类型为身份证且身份证号码不为空，则需要验证身份证号码有效性
    cred_type = form.form 'get value', 'acct_cred_type'
    cred_no   = form.form 'get value', 'acct_cred_no'
    if parseInt(cred_type) == 1 and cred_no.trim().length > 0
        result = validate_idno(cred_no, '收款账户开户人')
        if not result.succ
            alert result.errmsg
            return false

    # 账户姓名和证件号码必须提供一个
    acct_name = form.form 'get value', 'acct_name'
    if cred_no.trim().length == 0 and acct_name.trim().length == 0
        alert '账户开户人姓名和证件号码必须至少填写一个'
        return false

    # 验证卡号，不一定完全准确，因此只能给用户提示
    acct_no = form.form 'get value', 'acct_no'
    if acct_no.length > 0
        if not validate_cardno acct_no
            return confirm "收款账号可能存在错误，选择［取消］按钮返回检查，或［确定］按钮继续提交."

    return true


$ ->
    gather_info = {}

    $('form#form1').form
        inline: true,
        fields:
            merch_type:  optional: false, rules: [ type: 'empty' ]

    $('form#form1_1').form
        inline: true,
        fields:
            t20_merno:   optional: false, rules: [
                { type: 'empty' }, { type: 'doesntContain[ ]' }
            ]
            t20_termid:  optional: false, rules: [
                { type: 'empty' }, { type: 'doesntContain[ ]' }
            ]
            name:        optional: false, rules: [ type: 'empty' ]
            contact:     optional: false, rules: [ type: 'empty' ]
            mobile:      optional: true,  rules: [ type: 'integer[..]' ]
            address:     optional: false, rules: [ type: 'empty' ]
            email:       optional: true,  rules: [ type: 'email' ]
            tz0_merno:   optional: false, rules: [
                { type: 'minLength[4]'}, { type: 'doesntContain[ ]'}
            ]
            tz0_termid:  optional: false, rules: [
                { type: 'empty' }, { type: 'doesntContain[ ]' }
            ]
            tz0_macalgo: optional: false, rules: [ type: 'empty' ]
            tz0_tmk:     optional: false, rules: [
                { type: 'minLength[32]' }, { type: 'maxLength[32]' },
                { type: 'regExp[/^[0-9a-fA-F]+$/]' }
            ]

    $('form#form1_2').form
        inline: true,
        fields:
            t20_fee:        optional: false, rules: [ type: 'empty' ]
            acct_no:        optional: false, rules: [
                { type: 'empty' }, { type: 'integer[..]' },
            ]
            acct_name:      optional: true,  rules: [ type: 'doesntContain[ ]' ]
            acct_cred_type: optional: true,  rules: [ type: 'integer[..]' ]
            acct_mobile:    optional: true,  rules: [ type: 'integer[..]' ]
            pay_chnl:       optional: false, rules: [ type: 'empty' ]
            pay_merid:      optional: false, rules: [ type: 'empty' ]

    $('form#form1_3').form
        inline: true, fields: {}

    $('form#form1_4').form
        inline: true, fields: {}


    $('form#form2_1').form
        inline: true,
        fields:
            t60_merno:   optional: false, rules: [
                { type: 'empty' }, { type: 'doesntContain[ ]' }
            ]
            t60_termid:  optional: false, rules: [
                { type: 'empty' }, { type: 'doesntContain[ ]' }
            ]
            name:        optional: false, rules: [ type: 'empty' ]
            contact:     optional: false, rules: [ type: 'empty' ]
            mobile:      optional: true,  rules: [ type: 'integer[..]' ]
            address:     optional: false, rules: [ type: 'empty' ]
            email:       optional: true,  rules: [ type: 'email' ]
            tz1_merno:   optional: false, rules: [
                { type: 'minLength[4]' }, { type: 'doesntContain[ ]' }
            ]
            tz1_termid:  optional: false, rules: [
                { type: 'empty' }, { type: 'doesntContain[ ]' }
            ]
            tz1_macalgo: optional: false, rules: [ type: 'empty' ]
            tz1_tmk:     optional: false, rules: [
                { type: 'minLength[32]' }, { type: 'maxLength[32]' },
                { type: 'regExp[/^[0-9a-fA-F]+$/]' }
            ]

    $('form#form2_2').form
        inline: true,
        fields:
            t60_fee:        optional: false, rules: [ type: 'empty' ]
            p2_fund:        optional: false, rules: [ type: 'empty' ]
            fee_take_type:  optional: false, rules: [ type: 'empty' ]
            fund_lack_proc: optional: false, rules: [ type: 'empty' ]
            acct_no:        optional: false, rules: [
                { type: 'empty' }, { type: 'integer[..]' },
            ]
            acct_name:      optional: true,  rules: [ type: 'doesntContain[ ]' ]
            acct_cred_type: optional: true,  rules: [ type: 'integer[..]' ]
            acct_mobile:    optional: true,  rules: [ type: 'integer[..]' ]
            pay_chnl:       optional: false, rules: [ type: 'empty' ]
            pay_merid:      optional: false, rules: [ type: 'empty' ]

    $('form#form2_3').form
        inline: true, fields: {}

    $('form#form2_4').form
        inline: true, fields: {}


    # 第一步选择商户类型修改处理
    $('.modal.step1 .dropdown.merch_type').dropdown
        onChange: (value, text, $choice) ->
            $('.modal.step1 .button.next').data('next', "step#{value}_1")

    # 启动向导
    $('.ui.modal.step1').modal(closable: false).modal 'show'
    $('button.startup').click ->
        $('.ui.modal').modal 'hide all'
        $('.ui.modal.step1').modal 'show'

    # 下一步按钮事件
    $('.button.next').click ->
        form = $(this).parents('.ui.modal').find('form.form')
        return if not form.form 'is valid'

        # 如果是账户录入窗口，则需要验证账户信息
        if form.data('acct-info')
            return if not validate_acct_info form

        # 收集页面录入数据
        values = form.form 'get values'
        for k, v of values
            gather_info[k] = v

        next = $(this).data('next')
        if $(".ui.modal.#{next}").length == 0
            return alert '下一步窗口不存在，请检查...'
        $(".ui.modal.#{next}").modal('hide others')
            .modal(closable: false).modal 'show'

    $('.button.prev').click ->
        prev = $(this).data('prev')
        $(".ui.modal.#{prev}").modal('hide others')
            .modal(closable: false).modal 'show'

    $('.button.finish').click ->
        form = $(this).parents('.ui.modal').find('form.form')
        return if not form.form 'is valid'

        # 收集页面录入数据
        values = form.form 'get values'
        for k, v of values
            gather_info[k] = v

        action = $(this).data('action')
        $this = $(this).addClass 'loading'
        $.post action, info: gather_info, (resp) ->
            $this.removeClass 'loading'
            return if not resp.succ
            $this.parents('.modal').modal 'hide'
            gather_info = {}
            alert '新商户入户成功...'


    # 生成终端主密钥
    $('a#tmk_gen').click (evt) ->
        evt.preventDefault()
        key = ''
        for i in [0..31]
            t = Math.ceil(Math.random() * 15)
            t = if t > 9 then t - 10 + 65 else t + 48
            key += String.fromCharCode(t)
        $(this).siblings('input.tmk').val key

    # 改变代付渠道时，更新代付商户列表
    $('.dropdown.pay_chnl').dropdown
        onChange: (value, text, $choice) ->
            $(this).addClass 'loading'
            $.post '/wizard/ajax/pay_merids', {
                chnl: value
            }, (resp) ->
                $('.dropdown.pay_chnl').removeClass 'loading'
                return if not resp.succ
                $('.dropdown.pay_merid').dropdown 'clear'
                items = ''
                for merid in resp.merids
                    items += "<div class='item' data-value='#{merid.uuid}'>"
                    items += "#{merid.merno}"
                    items += "<div class='right floated'
                        style='font-size:small; color:#888;'>#{merid.name}"
                    items += "</div>"
                    items += "</div>"
                $('.dropdown.pay_merid .menu').html items
                $('.dropdown.pay_merid').dropdown 'refresh'

    # 交易权限全选按钮事件
    $('.ui.button#trcode_all').click ->
        $(this).parents('form').find('.checkbox.trcode').each ->
            $(this).checkbox 'check'

    # 交易权限全不选按钮事件
    $('.ui.button#trcode_none').click ->
        $(this).parents('form').find('.checkbox.trcode').each ->
            $(this).checkbox 'uncheck'

    # 交易控制模板改变
    $('.dropdown.trctl_t').dropdown
        onChange: (value, text, $choice) ->
            $form = $(this).parents('form')
            $this = $(this).addClass 'loading'
            $.post '/wizard/ajax/trctl', {
                trctl_uuid: value, trcode: $(this).data('trcode')
            }, (resp) ->
                $this.removeClass 'loading'
                return if not resp.succ
                $form.form 'set values', resp.trctl
